<template>
    <div>
         <!-- 精选 -->
        <div class="selected">
          <div class="w">
            <div class="selected-item">
              <div class="selected-dt"><span>{{selectData.name}}精选</span></div>
              <div class="selected-dd">
                <div class="dd-item">
                  <div class="item-title">{{selectData.name}}</div>
                    <a @click="moveSelectedList_l" class="iconfont arrow arrow-l" href="javascript:;">&#xe680;</a>
                    <a @click="moveSelectedList_r" class="iconfont arrow arrow-r" href="javascript:;">&#xe681;</a>
                  <div class="item-list">
                    <ul ref="selectedListRef" class="list-item">
                      <li @click="showPrivece(item)" :key="index" v-for="(item, index) in selectData.data">
                        <img v-lazy="`http://47.105.157.158:3000/images/cat/${item.cat}/${item.pathName}`" alt="">
                        <div class="space">
                          <span>{{item.albumName}}</span>
                        <div class="list-item-tools">
                          <i @click="submitDianZan(item._id, index, selectData.name, item)" class="dianzan iconfont">&#xe73f;</i>
                          <span>{{item.dianzan}}</span>
                        </div>
                        <div class="list-item-user">
                          <img 
                          v-lazy="item.user.avatar == 'no' ? 'http://47.105.157.158:3000/images/upload/default.jpg' : `http://47.105.157.158:3000/images/upload/${item.user.avatar}`">
                          <span>{{item.user.userName}}</span>
                        </div>
                        </div>
                      </li>
                    </ul>    
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
    </div>
</template>

<script>
import { ref, inject } from '@vue/composition-api';
import axios from 'axios';
export default {
  props: {
    selectData: {
      type: Object
    }
  },
    setup(props, context) {
        axios.defaults.baseURL = 'http://47.105.157.158:3000';
        // 精选列表DOM
        const selectedListRef = ref(null);
        // 点击左箭头移动
        const moveSelectedList_l = function () {
            selectedListRef.value.style.transform = 'translateX(0)';
        }
        // 点击右箭头移动
        const moveSelectedList_r = function () {
            selectedListRef.value.style.transform = 'translateX(-1200px)';
        }


        // 点赞
        const submitDianZan = async function (id, index, theName, item) {
          let { data: res } = await axios.post(`/set/dianzan/${id}`);
          if (res.status != 200)
            return context.parent.$message.error('点赞失败，遇到未知错误');
          context.parent.$message.success('点赞成功，要节制哦~');
          item.dianzan = ++item.dianzan;
        }

        // 显示预览
        const showPrivece = function (item) {
          // 传递给父组件
          context.emit('showPrivece', item);
        }

        const methods = { moveSelectedList_l, moveSelectedList_r, submitDianZan, showPrivece }

        return {
            ...methods,
            selectedListRef,
        }
    }
}
</script>


<style lang="less" scoped>
.selected {
  margin-top: 100px;
}

.selected-dt {
  text-align: center;
  margin-bottom: 60px;
  span {
    display: inline-block;
    padding-bottom: 10px;
    border-bottom: 2px solid #1a8eed;
  }
}

.dd-item {
  .item-title {
    font-size: 22px;
    margin-bottom: 20px;
    position: relative;
  }
  .arrow {
    position: absolute;
    top: 74%;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border-radius: 50%;
    background-color: #fff;
    transform: translateY(-50%);
    color: #585858;
    font-size: 30px;
    transition: all .6s;
    &:hover {
      background-color: #a2a3a3;
      color: #fff;
    }
   }
  .arrow-l {
    left: -74px;
   }
  .arrow-r {
    right: -74px;
   }
  .item-list {
    overflow: hidden;
    .list-item {
      display: flex;
      width: 2400px;
      transition: all .8s;
      li {
        width: 284px;
        margin-right: 18px;
        cursor: pointer;
        .space {
          padding: 0 10px;
        }
        img {
          width: 100%;
          height: 190px;
        }
        span {
          display: inline-block;
          margin: 10px 0;
          font-weight: 700;
          letter-spacing: 1px;
          font-size: 13px;
        }
        .list-item-tools {
          margin-bottom: 10px;
          span {
            font-weight: 400;
            margin: 0 4px;
            font-size: 12px;
          }
          .dianzan {
            cursor: pointer;
          }
        }
        .list-item-user {
          border-top: 1px solid #eee;
          padding: 10px;
          img {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            vertical-align: middle;
            margin-right: 20px;
          }
        }
      }
    }
  }
}
</style>